"""
File-based storage implementations
"""

"""
    FileStorage

Stores simulation data in JLD2 files.
"""
mutable struct FileStorage
    filename::String
    data::Dict{Float64, Any}
    times::Vector{Float64}
    
    function FileStorage(filename::String)
        storage = new(filename, Dict{Float64, Any}(), Float64[])
        
        # Try to load existing data
        if isfile(filename)
            try
                stored = load(filename)
                storage.data = get(stored, "data", Dict{Float64, Any}())
                storage.times = get(stored, "times", Float64[])
            catch
                # If loading fails, start with empty storage
            end
        end
        
        return storage
    end
end

function save_field(storage::FileStorage, field, time::Float64)
    storage.data[time] = deepcopy(field)
    if !(time in storage.times)
        push!(storage.times, time)
        sort!(storage.times)
    end
    
    # Save to file
    save(storage.filename, Dict("data" => storage.data, "times" => storage.times))
end

function load_field(storage::FileStorage, time::Float64)
    if haskey(storage.data, time)
        return storage.data[time]
    else
        throw(KeyError("Time $time not found in storage"))
    end
end

function get_time_series(storage::FileStorage)
    return copy(storage.times)
end

# Alias for compatibility
const HDFStorage = FileStorage